package com.pig4cloud.pig.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.Service.BuyCommentService;
import com.pig4cloud.pig.demo.feign.Entity.BuyComment;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 订单评价-控制器
 * @author pig code generator
 * @date 2020-11-03 14:42:09
 */
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/article/buyComment" )
public class BuyCommentController {

    private final BuyCommentService buyCommentService;

    /**
     * 根据用户id分页查询
     * @param page 分页对象
     * @param buyComment
     * @return
     */
    @GetMapping("/page" )
    public R getCompanyPage(Page page, BuyComment buyComment) {
        QueryWrapper<BuyComment> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", buyComment.getCid());
        return R.ok(buyCommentService.page(page, wrapper));
    }


    /**
     * 通过id查询订单评价
     * @param id id
     * @return R
     */
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(buyCommentService.getById(id));
    }

    /**
     * 新增或修改订单评价
     * @param buyComment
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @Inner(value = false)
    public R save(@RequestBody BuyComment buyComment) {

        buyComment.setPublishTime(new Date());

        QueryWrapper<BuyComment> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", buyComment.getCid());
        wrapper.eq("aid", buyComment.getAid());

        BuyComment comment = buyCommentService.getOne(wrapper);
        if (comment == null || comment.getId() == null) {
            return R.ok(buyCommentService.save(buyComment));
        }
        comment.setPublishTime(new Date());
        comment.setContent(buyComment.getContent());
        comment.setValue(buyComment.getValue());
        return R.ok(buyCommentService.updateById(comment));
    }

    /**
     * 修改订单评价
     * @param buyComment
     * @return R
     */
    @Inner(value = false)
    @PutMapping("/update")
    public R updateById(@RequestBody BuyComment buyComment) {
        return R.ok(buyCommentService.updateById(buyComment));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Long id) {
        return R.ok(buyCommentService.removeById(id));
    }

    /**
     * 根据用户id查询订单评价
     * @param id
     * @return
     */
    @Inner(value = false)
    @GetMapping("/getByUserId/{id}")
    public R getByUserId(@PathVariable("id") Long id){
        QueryWrapper<BuyComment> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", id);

        BuyComment buyComment = buyCommentService.getOne(wrapper);
        return R.ok(buyComment);
    }

}
